using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using QLVMBAYDomain;
namespace QLVMBAYDatacess
{
    public class ThanhPhoDA : DataAccessBase
    {
		#region Constructors
		
		public ThanhPhoDA() : base()
        {
			_selectSql = "Select * From THANHPHO";
            _insertSql = "Insert Into THANHPHO(MATP,MAQG,TENTP,ISMACDINH) Values (@MATP,@MAQG,@TENTP,@ISMACDINH)";
            _updateSql = "Update THANHPHO Set MAQG=@MAQG,TENTP=@TENTP,ISMACDINH=@ISMACDINH Where 1=1 And MATP=@MATP";
        	_deleteSql = "Delete From THANHPHO Where 1=1 And MATP=@MATP";
        	_primarSql = "Select * From THANHPHO Where 1=1 And MATP=@MATP";
            
        }
		
		protected static ThanhPhoDA _inst = null;
		public static ThanhPhoDA Inst
		{
			get
			{
				if(_inst == null)
					_inst = new ThanhPhoDA();
				return _inst;
			}
		}
		
		#endregion
		
        #region Get Methods

		private ThanhPho GetCurrent(IDataReader reader, bool isDeep)
		{
			ThanhPho ret = new ThanhPho();
			ret.MaThanhPho = (string)reader["MATP"];
			ret.TenThanhPho = (string)reader["TENTP"];
            ret.IsMacDinh = reader["ISMACDINH"] != null && reader["ISMACDINH"] != DBNull.Value ? (int)reader["ISMACDINH"] : 0;
			ret.ItemQuocGia = new QuocGia();
			if(isDeep)
			{
				ret.ItemQuocGia =  QuocGiaDA.Inst.GetByPrimaryKey(isDeep, (string)reader["MAQG"]);
			}
			else
                ret.ItemQuocGia.MaQG = (string)reader["MAQG"];
			
			
			return ret;
		}

		public List<ThanhPho> GetAll(bool isDeep)
		{
			DbCommand comm = GetCommand(_selectSql);
            using (Connection)
            {
                using (DbDataReader dr = comm.ExecuteReader())
                {
                    List<ThanhPho> ret = new List<ThanhPho>();
					while(dr.Read())
					{
						ret.Add(GetCurrent(dr, isDeep));
					}
					return ret;
                }
            }
		}
        public List<ThanhPho> GetListTPByQD(string MaQG,bool isDeep)
        {
            DbCommand comm = GetCommand("Select * From THANHPHO Where MAQG=@MAQG");
            SetParam(comm, "@MAQG", MaQG);
            using (Connection)
            {
                using (DbDataReader dr = comm.ExecuteReader())
                {
                    List<ThanhPho> ret = new List<ThanhPho>();
                    while (dr.Read())
                    {
                        ret.Add(GetCurrent(dr, isDeep));
                    }
                    return ret;
                }
            }
        }

        public List<ThanhPho> GetAllTPForDatVe(bool isDeep)
        {
            DbCommand comm = GetCommand("select distinct tp.* from THANHPHO tp join SANBAY sb on tp.MATP=sb.MATP");            
            using (Connection)
            {
                using (DbDataReader dr = comm.ExecuteReader())
                {
                    List<ThanhPho> ret = new List<ThanhPho>();
                    while (dr.Read())
                    {
                        ret.Add(GetCurrent(dr, isDeep));
                    }
                    return ret;
                }
            }
        }

		public ThanhPho GetByPrimaryKey(bool isDeep, params object[] args)
		{
			DbCommand cmd = GetCommand(_primarSql);
			if(args.Length < 1)
			{
				throw new Exception();
			}
            SetParam(cmd, "@MATP", args[0]);

            using (Connection)
            {
                using (DbDataReader dr = cmd.ExecuteReader())
                {
					if(dr.Read())
					{
						return GetCurrent(dr, isDeep);
					}
					return null;
                }
            }
		}
		
		public bool Insert(ThanhPho entity)
		{
			DbCommand cmd = GetCommand(_insertSql);
            SetParam(cmd, "@MATP", entity.MaThanhPho);
            SetParam(cmd, "@TENTP", entity.TenThanhPho);
            SetParam(cmd, "@ISMACDINH", entity.IsMacDinh);
			SetParam(cmd, "@MAQG", entity.ItemQuocGia.MaQG);
			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Update(ThanhPho entity)
		{
			DbCommand cmd = GetCommand(_updateSql);
            SetParam(cmd, "@MATP", entity.MaThanhPho);
            SetParam(cmd, "@TENTP", entity.TenThanhPho);
            SetParam(cmd, "@ISMACDINH", entity.IsMacDinh);
			SetParam(cmd, "@MAQG", entity.ItemQuocGia.MaQG);
			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Delete(ThanhPho entity)
		{
			DbCommand cmd = GetCommand(_deleteSql);
            SetParam(cmd, "@MATP", entity.MaThanhPho);

            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		#endregion
    }
}


